package cn.iocoder.yudao.module.train.controller.admin.courseclasses;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.train.controller.admin.courseclasses.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseClassesCommodityDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.VipClassesCommodityDTO;
import cn.iocoder.yudao.module.train.service.courseclasses.CourseClassesService;
import cn.iocoder.yudao.module.train.service.courseclasses.VipClassesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 班级")
@RestController
@RequestMapping("/train/vip-classes")
@Validated
public class VipClassesController {

    @Resource
    private VipClassesService vipClassesService;

    @PostMapping("/create")
    @Operation(summary = "创建班级")
    @PreAuthorize("@ss.hasPermission('train:vip-classes:create')")
    public CommonResult<Long> createCourseClasses(@Valid @RequestBody VipClassesSaveReqVO createReqVO) {
        return success(vipClassesService.createVipClasses(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新班级")
    @PreAuthorize("@ss.hasPermission('train:vip-classes:update')")
    public CommonResult<Boolean> updateCourseClasses(@Valid @RequestBody VipClassesSaveReqVO updateReqVO) {
        vipClassesService.updateVipClasses(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除班级")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:vip-classes:delete')")
    public CommonResult<Boolean> deleteVipClasses(@RequestParam("id") Long id) {
        vipClassesService.deleteVipClasses(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除班级")
                @PreAuthorize("@ss.hasPermission('train:vip-classes:delete')")
    public CommonResult<Boolean> deleteCourseClassesList(@RequestParam("ids") List<Long> ids) {
        vipClassesService.deleteVipClassesListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得班级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:vip-classes:query')")
    public CommonResult<VipClassesRespVO> getVipClasses(@RequestParam("id") Long id) {
        VipClassesCommodityDTO vipClasses = vipClassesService.getVipClasses(id);
        return success(BeanUtils.toBean(vipClasses, VipClassesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得班级分页")
    @PreAuthorize("@ss.hasPermission('train:vip-classes:query')")
    public CommonResult<PageResult<VipClassesRespVO>> getVipClassesPage(@Valid VipClassesPageReqVO pageReqVO) {
        PageResult<VipClassesCommodityDTO> pageResult = vipClassesService.getVipClassesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VipClassesRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得全部班级")
    @PreAuthorize("@ss.hasPermission('train:vip-classes:query')")
    public CommonResult<List<VipClassesRespVO>> getCourseClassesList(@Valid VipClassesPageReqVO pageReqVO) {
        List<VipClassesCommodityDTO> pageResult = vipClassesService.getVipClassesList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VipClassesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出班级 Excel")
    @PreAuthorize("@ss.hasPermission('train:vip-classes:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCourseClassesExcel(@Valid VipClassesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VipClassesCommodityDTO> list = vipClassesService.getVipClassesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "班级.xls", "数据", VipClassesRespVO.class,
                        BeanUtils.toBean(list, VipClassesRespVO.class));
    }

}
